/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlets;

import Classes.Immo;
import Classes.ShoppingChart;
import Classes.User;
import DAO.UserDAO;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Enumeration;
import java.util.Vector;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;

/**
 *
 * @author alexander
 */
public class UserManager extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       
        
        /*
         * Create new user and store it @ DB
         */
        
        if(request.getParameter("Quelle").equalsIgnoreCase("createNewUser")){
        
            String userAlias = request.getParameter("newUserAlias");
            String userEmailAddress = request.getParameter("newUserEmailAddress");
            String userPassword = request.getParameter("newUserPassword");
            String userPasswordRe = request.getParameter("newUserPasswordRe");
            String userFirstName = request.getParameter("newUserFirstName");
            String userLastName = request.getParameter("newUserLastName");
            System.out.println("Hier gehts lang");

            if(userPasswordRe.equalsIgnoreCase(userPassword)){

                UserDAO newUserDAO = new UserDAO();


                if (!newUserDAO.checkUserExists(userEmailAddress)){

                    User newUser = new User();
                    newUser.setUserID(1);
                    newUser.setUserAlias(userAlias);
                    newUser.setUserEMailAddress(userEmailAddress);
                    newUser.setUserFirstName(userFirstName);
                    newUser.setUserLastName(userLastName);
                    newUser.setUserPassword(userPassword);
                    //Die UserRole wird derzeit per default in der Datenbank
                    //bestimmt
                    //newUser.setUserRole(0);
                    UserDAO newUserDAO2 = new UserDAO();
                    newUserDAO2.saveUser(newUser);

                    response.sendRedirect("index.jsp");

                }else{
                    System.out.println("Sorry, aber ein User mit dieser E-Mail Adresse existiert bereits");
                    response.sendRedirect("createNewUser.jsp"); 
                }
            }
            
            /*
             * Falches Passwort
             */
            else {
               System.out.println("Sorry, aber falsches Passwort");
               response.sendRedirect("createNewUser.jsp"); 
            }
        }

         /*
         * User-Login 
         */
        
      if(request.getParameter("Quelle").equalsIgnoreCase("UserLogin")){
          
          System.out.println("Wir finden mal den richtigen Teil des Servlets ");
          
          String userAlias = request.getParameter("userAlias");
          String password = request.getParameter("userPassword");
          
          System.out.println("Funktioniert");
          
          System.out.println(userAlias);
          System.out.println(password);
          
          User newUser = new User();
          UserDAO newUserDAO = new UserDAO();
          
          User loginUser = newUserDAO.getUser(userAlias);
          
          System.out.println("HIER HIER HIER HIER HIER");
          System.out.println("User Alias:"+loginUser.getUserAlias());
          System.out.println("User ID:"+loginUser.getUserID());
          System.out.println("User FirstName:"+loginUser.getUserFirstName());
          System.out.println("User LastName:"+loginUser.getUserLastName());
          System.out.println("User E-Mail:"+loginUser.getUserEMailAddress());
          
          
          
          if(!newUserDAO.checkUserExists(userAlias)){
              System.out.println("Sorry, aber den User gibt es ja gar nicht");
          }
          
          
          else if(loginUser.getUserPassword().equalsIgnoreCase(password)){
              System.out.println("Willkommen im Club und Hallo");   
              HttpSession session = request.getSession(true);
              
              Date created = new Date(session.getCreationTime());
              String date = created.toString();
              
              ShoppingChart basket = new ShoppingChart();
              basket.setChartID(1);
              Vector<Immo> immoBasket = new Vector<Immo>();
              basket.setChartID(1);
              basket.setImmoInTheBasket(immoBasket);
              basket.setUserIDisOwnerOfShoppignChart(loginUser.getUserID());

              session.setAttribute("sessionID", session.getId());
              session.setAttribute("createdAT", date);
              session.setAttribute("sessionUserAlias", loginUser.getUserAlias());
              session.setAttribute("sessionUserID", loginUser.getUserID());
              session.setAttribute("sessionUserFirstName", loginUser.getUserFirstName());
              session.setAttribute("sessionUserLastName", loginUser.getUserLastName());
              session.setAttribute("sessionUserEMail", loginUser.getUserEMailAddress());
              session.setAttribute("sessionUserPwd",loginUser.getUserPassword());
              session.setAttribute("sessionUserRole", loginUser.getUserRole());
              session.setAttribute("sessionBasket", basket);
              
              
              response.sendRedirect("index.jsp");
    
          }
          else if(!loginUser.getUserPassword().equalsIgnoreCase(password)){
              System.out.println("Falsches Passwort");   
          }

      }
      
      /*
       * User-Logout
       */
      
      if(request.getParameter("Quelle").equalsIgnoreCase("UserLogout")){
           System.out.println("Logout");
           
           HttpSession session = request.getSession(false);
           if(session!=null){
             session.invalidate();
           }
           
           response.sendRedirect("index.jsp");

    }
   }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
